package com.lsfb.signsys.utils;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;

import com.google.common.util.concurrent.ServiceManager;
import com.lsfb.signsys.my.MyInfoBean;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.subjects.ReplaySubject;
import io.reactivex.subjects.Subject;

/**
 * Created by zgscwjm on 2018/4/2.
 */

public class Utils {

    private static Utils mUtils;


    private Integer num = 0;

    private Subject mSubject = ReplaySubject.createWithSize(1);

    private MyInfoBean mMyInfoBean2;

    ObservableEmitter<MyInfoBean> mEitter = null;

    public Observable<MyInfoBean> getUPdate() {
        return mSubject;
    }


    public void updateUser(MyInfoBean myInfoBean) {
        this.mMyInfoBean2 = myInfoBean;
        mSubject.onNext(myInfoBean);


    }


    public static Utils getInstall() {
        if (null == mUtils) {
            mUtils = new Utils();
        }
        return mUtils;
    }

//    private Observable<MyInfoBean> mInfoBean;
    private ConnectableObservable<MyInfoBean> mInfoBean;

    public Observable<MyInfoBean> getInfoBean() {

        if (mInfoBean == null) {
            ObservableOnSubscribe observableOnSubscribe = new ObservableOnSubscribe<MyInfoBean>() {

                @Override
                public void subscribe(ObservableEmitter<MyInfoBean> emitter) throws Exception {
                    mEitter = emitter;
                }

            };
            mInfoBean = Observable.create(observableOnSubscribe).replay(1);
            mInfoBean.connect();
        }
        return mInfoBean;
    }

    public void subscript(Consumer<MyInfoBean> consumer) {

        getInfoBean().subscribeOn(AndroidSchedulers.mainThread()).subscribe(consumer);

    }

    public void setInfoBean(MyInfoBean infoBean) {

        if (mEitter == null) {
            getInfoBean();

        }

        mEitter.onNext(infoBean);


    }


}
